<?php
/*\
 *	
 *	
 *	
 *	
\*/

function &oh_model_new_class($query,$data) {
	if (!oh("model")->is_ready()) return oh("model");

	$a = oh_model_get_args($query,$data,"name","define:form");
	$p = oh_model_get_prefix();	
	$i = oh_model_get_insert($a,"class");
	$q = "INSERT INTO {$p}class({$i[fields]}) VALUES ({$i[values]})";
	
	if ($a->get == "query") {
		return $q;
	}	
	if (!oh("model")->hit_class($a)) {
		oh("model")->query($q);					
	}		
	return oh("model");
}

function &oh_model_hit_class($query,$data=null) {
	if (!oh("model")->is_ready()) return oh("model");

	$a = oh_model_get_args($query,$data,"name","define:form");
	$p = oh_model_get_prefix();	
	$i = oh_model_get_insert($a,"class");
	$q = "SELECT id FROM {$p}class WHERE name='{$a->name}' LIMIT 1";
	
	if ($a->get == "query") {
		return $q;
	}	
	$r = oh("model")->query($q,true);					
	return $r;
}

function &oh_model_get_class($query,$data=null) {
	if (!oh("model")->is_ready()) return oh("model");

	$a = oh_model_get_args($query,$data,"name","define:form");
	$p = oh_model_get_prefix();	
	$q = "SELECT * FROM {$p}class WHERE name='{$a->name}'";
	
	if ($a->get == "query") {
		return $q;
	}	
	$r = oh("model")->query($q,true);
	foreach($r as &$i) {
		$i = oh_model_get_prototype($i);
	}
	return $r;
}

